const db = require("../../config/db");

//导入加密/解密文件
const aes2 = require("../../utils/aes2");
//导入jwt token工具
const jwt = require("../../utils/jwt");

//默认头像
let { HOST_URL } = require("../../config/host");
let default_img_arr = ["/images/11.jpg", "/images/22.jpg", "/images/33.jpg", "/images/44.jpg", "/images/55.jpg", "/images/66.jpg"];
let rd = () => {
    return Math.floor(Math.random() * default_img_arr.length);
};

module.exports = {
    //注册
    register({ user_id, user_nick_name, user_acc, user_pwd, user_create_time }) {
        return new Promise((resolve, reject) => {
            let check_user_sql = `select * from db_user where user_acc="${user_acc}"`;
            db.query(check_user_sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "查询用户异常",
                        err,
                    });
                } else {
                    // console.log(rows);
                    if (rows.length == 0) {
                        let pwd = aes2.aesEncrypt(user_pwd);
                        let user_avators = `${HOST_URL}${default_img_arr[rd()]}`;
                        let insert_user_sql = `insert into db_user(user_id,user_acc,user_pwd,user_nick_name,user_avators,user_create_time) values("${user_id}","${user_acc}","${pwd}","${user_nick_name}","${user_avators}","${user_create_time}")`;
                        db.query(insert_user_sql, (error, result) => {
                            if (error) {
                                reject({
                                    code: -1,
                                    msg: "用户注册失败",
                                    error,
                                });
                            } else {
                                resolve({
                                    userInfo: {
                                        user_id,
                                        user_nick_name,
                                    },
                                });
                            }
                        });
                    } else {
                        resolve({
                            code: 201,
                            msg: "用户已经存在",
                        });
                    }
                }
            });
        });
    },

    //登录
    login({ user_acc, user_pwd }) {
        return new Promise((resolve, reject) => {
            let check_acc_sql = `select * from db_user where user_acc="${user_acc}"`;
            db.query(check_acc_sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "查询登录用户异常",
                    });
                } else {
                    if (rows.length == 1) {
                        if (aes2.aesDecrypt(user_pwd, rows[0].user_pwd)) {
                            let { user_id, user_nick_name, user_avators, user_message } = rows[0];
                            resolve({
                                code: 200,
                                msg: "登录成功",
                                userInfo: {
                                    user_id,
                                    user_nick_name,
                                    user_avators,
                                    user_message,
                                },
                            });
                        } else {
                            resolve({
                                code: -1,
                                msg: "账号或密码输入不正确",
                            });
                        }
                    } else if (rows.length == 0) {
                        resolve({
                            msg: "账号不存在",
                        });
                    } else {
                        resolve({
                            code: 500,
                            msg: "系统异常。",
                        });
                    }
                }
            });
        });
    },

    //增加浏览记录
    addlook({ user_id, news_id, news_category, news_tag, news_title, news_img, news_time, news_media, user_avators }) {
        return new Promise((resolve, reject) => {
            let check_look_sql = `select * from db_look where user_id="${user_id}" and news_id="${news_id}"`;
            db.query(check_look_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    if (rows.length == 0) {
                        let insert_look_sql = `insert into db_look(user_id,news_id,news_category,news_tag,news_title,news_img,news_time,news_media,user_avators) values("${user_id}","${news_id}","${news_category}","${news_tag}","${news_title}","${news_img}","${news_time}","${news_media}","${user_avators}")`;
                        db.query(insert_look_sql, (error, result) => {
                            if (error) {
                                reject(error);
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "添加浏览记录",
                                });
                            }
                        });
                    }
                }
            });
        });
    },
    //删除记录
    removelook({ user_id, news_id }) {
        return new Promise((resolve, reject) => {
            let check_look_sql = `select * from db_look where user_id="${user_id}" and news_id="${news_id}"`;
            db.query(check_look_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    if (rows.length >= 1) {
                        let remove_look_sql = `DELETE FROM db_look WHERE user_id="${user_id}" and news_id="${news_id}"`;
                        db.query(remove_look_sql, (error, result) => {
                            if (error) {
                                reject(error);
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "删除记录成功",
                                });
                            }
                        });
                    } else {
                        resolve({
                            code: 201,
                            msg: "找不到浏览记录",
                        });
                    }
                }
            });
        });
    },

    //获取浏览记录
    getlook({ user_id }) {
        return new Promise((resolve, reject) => {
            let check_look_sql = `select * from db_look where user_id="${user_id}"`;
            db.query(check_look_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },

    //获取个人信息
    getUserInfo({ user_id }) {
        return new Promise((resolve, reject) => {
            let check_user_sql = `select * from db_user where user_id="${user_id}"`;
            db.query(check_user_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },
    // 修改用户头像（上传图片）
    modify_avator({ user_id, user_nick_name, user_message, user_avators }) {
        // console.log({user_id, user_avator});
        return new Promise((resolve, reject) => {
            let usr_check_sql = `select * from db_user where user_id = "${user_id}"`;
            db.query(usr_check_sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "查询用户失败",
                        err,
                    });
                } else {
                    if (rows.length == 1) {
                        let user_modify_sql = `update db_user set user_nick_name="${user_nick_name}",user_message="${user_message}" ,user_avators= "${user_avators}" where user_id= "${user_id}"`;
                        db.query(user_modify_sql, (error, result) => {
                            // console.log("9.0", { user_id, user_avator, error });
                            if (error) {
                                reject({
                                    code: -1,
                                    msg: "修改用户头像失败。",
                                    error,
                                });
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "修改用户头像成功。",
                                    user_nick_name,
                                    user_message,
                                    user_avators,
                                });
                            }
                        });
                    }
                }
            });
        });
    },

    // 获取用户头像和昵称
    get_avator_nickname(user_id) {
        return new Promise((resolve, reject) => {
            let sql = `select user_avator,user_nick_name from db_user where user_id = "${user_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "获取头像昵称失败。",
                    });
                } else {
                    reject({
                        code: 200,
                        msg: "获取头像昵称成功。",
                        data: rows,
                    });
                }
            });
        });
    },

    // 修改用户资料
    modify(user_id, user_age, user_addr, user_phone, user_sex, user_nick_name) {
        return new Promise((resolve, reject) => {
            var modify_info_sql = `update db_user set user_addr= "${user_addr}", user_age= "${user_age}", user_phone= "${user_phone}",user_sex= "${user_sex}",user_nick_name= "${user_nick_name}" where  user_id= "${user_id}"`;
            db.query(modify_info_sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "修改用户资料失败。",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "修改用户资料成功。",
                    });
                }
            });
        });
    },

    //获取用户发布的新闻
    getUserNews({ user_id }) {
        return new Promise((resolve, reject) => {
            let check_news_sql = `select * from db_news where user_id="${user_id}" limit 20`;
            db.query(check_news_sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
    },
};
